/**
 * 
 * JShakki  - Dude with recycled paper.
 * 
 * @version 1.0
 * 
 * Copyright (C) 2006 Jarkko Sipilä
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * 
 */

package dude.gui;
import javax.swing.JPanel;

import dude.data.Square;


import java.awt.*;

public class SquareComponent extends JPanel {
  
  private static final long serialVersionUID = 1L;
  
  private boolean o = false;
  private boolean x = false;
  private boolean horizontal = false;
  private boolean vertical = false;
  private boolean upward = false;
  private boolean downward = false;
  
  private Dimension coordinates;
  
  
  protected SquareComponent( Dimension d ) {
    super();
    this.coordinates = d;
  }
  
  
  public void paintComponent( Graphics g ) {
    super.setBackground( Color.WHITE );
    super.paintComponent( g );
    
    Insets insets = getInsets();
    
    Dimension d = this.getSize();
    int height = d.height;
    int width = d.width;
    
    int marginal = (height + width) / 20; // marginaali
    
    // ruudun reunukset
    g.setColor( Color.GRAY );
    g.drawRect( insets.left, insets.top, width, height );
    
    // ympyrä tai rasti
    if ( this.o ) {
      g.setColor( Color.BLACK );
      g.drawOval( insets.right+marginal, insets.bottom+marginal, width-marginal*2, height-marginal*2 );
    }
    else if ( this.x ) {
      g.setColor( Color.BLACK );
      g.drawLine( insets.left+marginal, insets.top+marginal, width-marginal, height-marginal );
      g.drawLine( marginal, height-marginal, width-marginal, marginal);
    }
    
    // viivat
    g.setColor( Color.RED );
    if( this.downward )
      g.drawLine( insets.left, insets.top, width, height ); // yläkulmasta alas
    if ( this.upward ) 
      g.drawLine( 0, height, width, 0); // alakulmasta ylös
    if ( this.horizontal )
      g.drawLine( insets.left, height/2, width, height/2 ); // vaaka	
    if ( this.vertical )
      g.drawLine( width/2, 0, width/2, height ); // pysty
    
  }
  
  
  protected void drawO() {
    if( !this.x && !this.o )
      this.o = true;
    this.repaint();
  }
  
  protected void drawX() {
    if( !this.x && !this.o )
      this.x = true;
    this.repaint();
  }
  
  protected void drawLine( int direction ) {
    switch ( direction ) {
    case Square.HORIZONTAL:
      this.horizontal = true;
      break;
    case Square.UPWARD:
      this.upward = true;
      break;
    case Square.VERTICAL:
      this.vertical = true;
      break;
    case Square.DOWNWARD:
      this.downward = true;
      break;
    }
    this.repaint();
  }
  
  protected Dimension getCoordinates() {
    return coordinates;
  }	
}

